package com.cskaoyan.service;

import com.cskaoyan.bean.Storage;
import com.cskaoyan.bean.StorageListBean;
import com.cskaoyan.bean.StorageVO;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.mapper.StorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @USER: cuiweiguo#
 * @DATE: 2022/5/6
 * @TIME: 15:18
 * @PROJECT_NAME: mallproject2
 */
@Service
public class StorageServiceImpl implements StorageService {
    @Autowired
    StorageMapper storageMapper;
    @Override
    public StorageVO storage(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();//name
        String contentType = file.getContentType();//type
        int size = (int) file.getSize();//size
        String uuid = UUID.randomUUID().toString();
        String key  = uuid.substring(0,20);//key

        try {
            file.transferTo(new File("src/main/resources/static/"+key));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //http://localhost:8083/wbb1.jpeg
        //https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif
       String url = "http://localhost:8083/"+key;//TODO:这里要重写---改成wv--fetch方法
        Date date = new Date(System.currentTimeMillis());

        Storage storage = new Storage(null, key, originalFilename, contentType, size, url, date, date, null);
        //插入数据库
        int affectedRows = storageMapper.insertSelective2(storage);
        if (affectedRows==1){
            StorageVO storageVO = new StorageVO(storage.getId(), storage.getKey(),storage.getName(),storage.getType(),storage.getSize(),storage.getUrl(),storage.getAddTime(),storage.getUpdateTime());
            return storageVO;
        }else{
            return null;
        }
    }

    @Override
    public StorageListBean list(String name, String key, BaseParam baseParam) {
        //查询总共所有的数量
        Integer page = baseParam.getPage();//页码
        Integer limit = baseParam.getLimit();//数据量
        String sort = baseParam.getSort();//排序列
        String order = baseParam.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息
        //  username="%"+username+"%";
        List<Storage> list = storageMapper.select(sort,order,name,key);
        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<Storage> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return StorageListBean.data(total, pages, limit, page, list);
    }

    @Override
    public int delete(Storage storage) {
        //虚假删除
        Storage newStorage = new Storage(storage.getId(), null, null, null, null, null, null, new Date(System.currentTimeMillis()), true);
        int affectedRows = storageMapper.updateByPrimaryKeySelective(newStorage);
        if (affectedRows==1){
            return 200;
        }
        return 0;
    }

    @Override
    public Storage update(Storage storage) {
        Storage newStorage = new Storage(storage.getId(), null, storage.getName(), null, null, null, null,
                new Date(System.currentTimeMillis()), null);
        int affectedRows = storageMapper.updateByPrimaryKeySelective(newStorage);
        if (affectedRows==1){
            return newStorage;
        }
        return null;
    }
}
